package com.baaio.conn;

import static com.baaio.util.Out.print;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.baaio.properties.DriverItem;
import com.baaio.util.DBInfo;
import com.baaio.util.GetInfo;
import com.baaio.util.Out;
/**
 * 建立数据库连接的类！<br>
 * 该类会被无数次调用，每次生成的连接connection和statement都会保存到map里面<br>
 * 待使用完毕后调用close方法统一销毁！
 *
 */
public class GetStatement
{

	private Connection dbconn = null;
	private Statement dbstate = null;
	private String jdbcurl = null;
	private String driverName = null;
	private String dbName = null;
	private static HashMap<Connection, Statement> map = new HashMap<Connection, Statement>();
	private static HashMap<Connection, Statement> map2 = new HashMap<Connection, Statement>();
	private boolean flag1 = false;

	protected GetStatement(String DBName)
	{

		setDbName(DBName);
	}

	protected GetStatement(String DBName, boolean flag)
	{

		setDbName(DBName);
		this.flag1 = flag;
	}

	protected Statement getStatement()
	{

		String userName = DBInfo.ROOTNAME.toString();
		String Psw = DBInfo.PASSWOROD.toString();

		if (driverName == null || jdbcurl == null)
		{
			driverName = DBInfo.DRIVENAME.toString();
			if(getDbName() != null)
			{
				GetInfo gif = GetInfo.getInstance();
				jdbcurl = DriverItem.getJDBCUrl(gif.getHost(),gif.getPort(),getDbName(),gif.getSqlORmysql());
				Out.print("消息1: " + jdbcurl);
			}
			else
			{
				
			jdbcurl = DBInfo.JDBCURL.toString();
			Out.print("消息2: " + jdbcurl);
			}
		}
		try
		{
			Class.forName(driverName).newInstance();
			dbconn = DriverManager.getConnection(jdbcurl, userName, Psw);
			dbstate = dbconn.createStatement();

			if (flag1)
			{
				map2.put(dbconn, dbstate);
			}
			else
			{
				map.put(dbconn, dbstate);
			}
		}
		catch (Exception e)
		{
			print("数据库连接失败！");
			print("url = " + jdbcurl); // 发生错误时，将连接数据库信息打印出来
			print("userName = " + userName);
			print("Psw = " + Psw);
			print("Exception: " + e.getMessage());// 得到出错信息
			print("请检查是否导入了数据库连接池jar包！");
		}

		return dbstate;

	}

	/**
	 * 断开数据库
	 * 
	 * @return boolean值，成功返回true，失败返回false
	 */
	protected static void dbClose()
	{

		int kk = 1;
		Set<Connection> set = map.keySet();
		for (Iterator<Connection> i = set.iterator(); i.hasNext();)
		{
			Connection conn = i.next();
			Statement statem = map.get(conn);
			try
			{
				statem.close();
				conn.close();
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			kk++;
		}
		print("数据连接关闭：" + kk);
		map.clear();
	}

	protected static void dbTClose()
	{

		int kk = 1;
		Set<Connection> set = map2.keySet();
		for (Iterator<Connection> i = set.iterator(); i.hasNext();)
		{
			Connection conn = i.next();
			Statement statem = map2.get(conn);
			try
			{
				statem.close();
				conn.close();
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			kk++;
		}
		print("数据连接关闭：" + kk);
		map2.clear();
	}

	protected String getDbName()
	{

		return dbName;
	}

	protected void setDbName(String dbName)
	{

		this.dbName = dbName;
	}

	protected Connection getDbconn()
	{

		return dbconn;
	}

	protected Statement getDbstate()
	{

		return dbstate;
	}

}
